package com.guoyansoft.model.service.impl;

import com.guoyansoft.config.R;
import com.guoyansoft.model.dao.GoodsAndShopDao;
import com.guoyansoft.model.dao.GoodsDao;
import com.guoyansoft.model.pojo.Goods;
import com.guoyansoft.model.pojo.GoodsAndShop;
import com.guoyansoft.model.pojo.Shop;
import com.guoyansoft.model.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsAndShopDao goodsAndShopDao;
    @Override
    public R selectByPageAndTotalsize(Map<String, Object> params) {
        List<Goods> shopList=goodsDao.selectByshopName(params);
        long total=goodsDao.selectTotalSize(params);
        return R.suc(total,shopList);
    }
    @Override
    public R selectByPageAndshopId(Map<String, Object> params) {
        List<Goods> shopList=goodsDao.selectByshopId(params);
        long total=goodsDao.selectTotalByshopId(params);
        return R.suc(total,shopList);
    }


    @Override
    public R update(Goods goods){
        int row = goodsDao.update(goods);
        return row>0?R.suc():R.fail();
    }

    @Override
    public R queryByPage(Map<String, Object> params)  {
        List<Goods> empList = goodsDao.selectByPage(params);
        long totalSize = goodsDao.selectTotalSize(params);
        return R.suc(totalSize,empList);
    }


    @Override
    public R dropBatch(Integer[] ids,Integer shopId) {
        Shop shop=new Shop();
        shop.setId(shopId);
        Map<String,Object> map=new HashMap<>();
        map.put("shop",shop);
        int row1=0;
        for (Integer id : ids) {
            map.put("ids",id);
        row1 += goodsAndShopDao.deleteByShopId(map);
        }
        int row2 = goodsDao.deleteBatch(ids);
        return row1>0&&row2>0?R.suc():R.fail();
    }

    @Override
    public R save(Goods goods,Integer shopId)  {
        Shop shop=new Shop();
        shop.setId(shopId);
        int row2 = goodsDao.insert(goods);
//        goodsDao.
        int row1 = goodsAndShopDao.insert(new GoodsAndShop(goods, shop));
        return row1>0&&row2>0?R.suc():R.fail();
    }
    @Override
    public Goods queryById(Integer id) {
        return goodsDao.selectById(id);
    }


    @Override
    public R selectByShopTypeId(Map<String,Object> params) {
        List<Goods> goods = goodsDao.selectByShopTypeId(params);
        Long total = goodsDao.selectTotalByShopTypeId(params);
        return R.suc(total,goods);
    }

    @Override
    public Goods queryByGoodsName(String goodsName) {
        Goods goods = goodsDao.selectByGoodsName(goodsName);
        return goods;
    }

    @Override
    public R queryPageAndShopId(Map<String, Object> params) {
        Long total = goodsDao.selectTotalByShopTypeId(params);
        List<Goods> goods = goodsDao.selectPageAndShopId(params);
        return R.suc(total,goods);
    }
}
